package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.impl.ChangeLogDaoImpl;
import cn.topevery.garden.dal.dao.impl.PlaneUseMaterialDaoImpl;
import cn.topevery.garden.dal.dao.impl.ScheduleFileDaoImpl;
import cn.topevery.garden.dal.logic.PlaneLogic;
import cn.topevery.garden.dal.util.ArrayListUtils;
import cn.topevery.garden.entity.dto.PlaneDTO;
import cn.topevery.garden.entity.enums.PlaneExmainState;
import cn.topevery.garden.entity.po.Plane;
import cn.topevery.garden.entity.po.PlaneUseMaterial;
import cn.topevery.garden.entity.po.ScheduleFile;
import cn.topevery.garden.entity.query.PlaneQuery;
import cn.topevery.garden.entity.vo.PlaneVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author code-generate
 * @date 2022-01-24
 */
@Api(tags = "")
@RestController
@RequestMapping("/plane")
public class PlaneController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    PlaneLogic planeLogic;

    @Autowired
    ScheduleFileDaoImpl scheduleFileMapper;

    @Autowired
    PlaneUseMaterialDaoImpl planeUseMaterialMapper;

    @Autowired
    ChangeLogDaoImpl changeLogMapper;


    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/list")
    public JsonResult<DataPager<PlaneVO>> listPage(@RequestBody PlaneQuery query) {
        return JsonResult.ok(planeLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody PlaneDTO dto) {
        planeLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, Plane.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<PlaneVO> getInfoById(@RequestParam Integer id) {
        Plane entity = planeLogic.getEntity(id);
        PlaneVO vo = mapper.map(entity, PlaneVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        planeLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(PlaneQuery query, HttpServletResponse response) {
        List<PlaneVO> list = planeLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", PlaneVO.class, "", response);
    }

    @GetMapping("/info")
    public JsonResult<Map<String, Object>> info(String planeId, String isVerify, String isCheck) {
        Map<String, Object> res = planeLogic.info(planeId, isVerify, isCheck);
        return JsonResult.ok(res);
    }


    @PostMapping("/insert")
    public JsonResult insert(@RequestBody Plane plane) {
        plane.setPlaneId(UUID.randomUUID().toString());
        plane.setDbStatus(false);
        plane.setExmainState(PlaneExmainState.notSubmitted.getIndex());
        plane.setExmainStateDesc(PlaneExmainState.notSubmitted.getLabel());
        planeLogic.insertList(getUser().id.toString(), plane, plane.getMaterials());
        return JsonResult.ok(true);
    }

    @GetMapping("/submitPlane")
    public JsonResult<String> submitPlane(String planeIds) {
        String res = planeLogic.submitPlane(planeIds, getUser().id.toString());
        return JsonResult.ok(res);
    }

    @PostMapping("/insertWater")
    public JsonResult insertWater(@RequestBody Plane plane) {
        plane.setPlaneId(UUID.randomUUID().toString());
        plane.setDbStatus(true);
        plane.setExmainState(0);
        plane.setExmainStateDesc("未提交");
        planeLogic.insert(getUser().id.toString(), plane);
        List<ScheduleFile> attaches = plane.getAttaches();
        if (attaches != null) {
            attaches.forEach(item -> {
                item.setDbStatus(true);
                item.setGuidId(plane.getPlaneId());
                item.setDateType(0);
                item.setFileTypeId(0);
                scheduleFileMapper.insert(getUser().id.toString(), item);
            });
        }
        return JsonResult.ok(true);

    }

    @PostMapping("/updateWater")
    public JsonResult updateWater(@RequestBody Plane plane) {
        planeLogic.update(getUser().id.toString(), plane);
        //先删除
        scheduleFileMapper.updateByTable(getUser().id.toString(), new ArrayListUtils<KeyValue>()
                        .addObject(new KeyValue("c_db_status", true)),
                new ArrayListUtils<KeyValue>()
                        .addObject(new KeyValue("c_guid_id", plane.getPlaneId())));
        List<ScheduleFile> attaches = plane.getAttaches();
        if (attaches != null) {
            attaches.forEach(item -> {
                item.setDbStatus(true);
                item.setGuidId(plane.getPlaneId());
                item.setDateType(0);
                item.setFileTypeId(0);
                scheduleFileMapper.insert(getUser().id.toString(), item);
            });
        }
        return JsonResult.ok(true);
    }

    @GetMapping("/getWater")
    public JsonResult<Plane> getEntityWater(String planeId) {
        Plane plane = planeLogic.getEntityByColumn("c_plane_id", planeId);
        if (plane != null) {
            List<ScheduleFile> attaches = scheduleFileMapper.getListEntityByOneColumn(new KeyValue("c_guid_id", planeId));
            plane.setAttaches(attaches);

            //详细
            List<PlaneUseMaterial> materials = planeUseMaterialMapper.getPlaneMaterials(planeId);
            plane.setMaterials(materials);
            //日志
            List<Map<String, Object>> logs = changeLogMapper.getLogByObjId(plane.getId());
            plane.setLogs(logs);
        }
        return JsonResult.ok(plane);
    }

}
